# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=octopi
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
#_qmake=${MINGW_PREFIX}/bin/qmake
_qmake=${MINGW_PREFIX}/qt5-static/bin/qmake
pkgver=r1108.46131e9
pkgrel=1
pkgdesc="a powerful Pacman frontend using Qt libs"
arch=('any')
url="https://github.com/aarnt/octopi"
license=('GPL2')
makedepends=('git' "${MINGW_PACKAGE_PREFIX}-qt5-static" "${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_gitroot="https://github.com/aarnt/octopi.git"
_gitname="octopi"
#options=('debug' '!strip')
source=("${_gitname}"::"git+${_gitroot}"
        "0001-Use-Q_PID-instead-of-int-reducing-QProcess-repititio.patch"
        "0002-Use-a-shared-QProcessEnvironment.patch"
        "0003-Win32-Link-to-Windows-Terminal-Services-wtsapi32.patch"
        "0004-Remove-redundancy-in-UnixCommand-s-exe-checking-func.patch"
        "0005-Use-instead-of-QDir-separator.patch"
        "0006-Append-to-instead-of-overwrite-m_readAllStandardOutp.patch"
        "0007-Define-QT_WA.patch"
        "0008-MSYS2-Add-ectn_MSYS2-as-a-LinuxDistro.patch"
        "0009-MSYS2-Add-getMSYS2Root.patch"
        "0010-MSYS2-hasPacmanDatabase-getMSYS2Root-changes.patch"
        "0011-MSYS2-Changes-to-various-system-calls.patch"
        "0012-MSYS2-Process-id-changes-WIP.patch"
        "0013-MSYS2-Prepend-getMSYS2Root-to-file-checks.patch"
        "0014-MSYS2-Support-discoverBinaryPath.patch")
sha256sums=('SKIP'
            '12cafba069ad175952e6187c0c8dd8dad03590cb350f3ca52c55932665225965'
            'a5c8820f608107219eb8256a6d462ca7e1a89ce7505fb2da6fe3de36f6bb3f4f'
            '4e956561f523877ad93fe4655e18e22d07ff79c43f968971848e37732aacfaac'
            'ac7162e7c4048e7b9043b6201e296ceaabcc68ccaf4fdfd249e17404b73d0651'
            'd2d3b1d35deedd4b392e6d6c05941e2c68abc562267189ac162be6d20ad510f5'
            'fb1dffea3e868dd13ccd56091d5bb03da52af4906495339f02de80f6f9c1cae3'
            'f2793bad2d5e6d99230f9275a13dcae1743b9aeefe10b2e312f56d99adcf01d2'
            'e05c33f81544315adb65e8d3cc879c6f32c27f39e226683544c044e4204bc779'
            '5ad9e70d2811c8ee5dd8247b0ff4f652a0400d2612331de20a5046c98aed1ac6'
            'bfda0478c4ac13d331b741e0c940cb5a8e277e8ac5b13d1de42a51b49a227fc6'
            '98c286e478526d54a0d6f1bb45a28cfec5df1128c6c1749fa8f3c287ba8e7d76'
            '2aa8e398290b02f58165b61e44452c9e7ed3ff45a94d3762b44b24a0a5b62b5b'
            'ae352241bcdf458b4eb6fffb7062815d0c8c819a4dad31e1a1a52e486cb475bf'
            'ae7a441625b404af080925fc43517ed8d65fe9b91d54cb219461a57312cf19c6')

pkgver() {
    cd "${srcdir}/${_realname}"
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_gitname}"

  git am --committer-date-is-author-date "${srcdir}"/0001-Use-Q_PID-instead-of-int-reducing-QProcess-repititio.patch
  git am --committer-date-is-author-date "${srcdir}"/0002-Use-a-shared-QProcessEnvironment.patch
  git am --committer-date-is-author-date "${srcdir}"/0003-Win32-Link-to-Windows-Terminal-Services-wtsapi32.patch
  git am --committer-date-is-author-date "${srcdir}"/0004-Remove-redundancy-in-UnixCommand-s-exe-checking-func.patch
  git am --committer-date-is-author-date "${srcdir}"/0005-Use-instead-of-QDir-separator.patch
  git am --committer-date-is-author-date "${srcdir}"/0006-Append-to-instead-of-overwrite-m_readAllStandardOutp.patch
  git am --committer-date-is-author-date "${srcdir}"/0007-Define-QT_WA.patch
  git am --committer-date-is-author-date "${srcdir}"/0008-MSYS2-Add-ectn_MSYS2-as-a-LinuxDistro.patch
  git am --committer-date-is-author-date "${srcdir}"/0009-MSYS2-Add-getMSYS2Root.patch
  git am --committer-date-is-author-date "${srcdir}"/0010-MSYS2-hasPacmanDatabase-getMSYS2Root-changes.patch
  git am --committer-date-is-author-date "${srcdir}"/0011-MSYS2-Changes-to-various-system-calls.patch
  git am --committer-date-is-author-date "${srcdir}"/0012-MSYS2-Process-id-changes-WIP.patch
  git am --committer-date-is-author-date "${srcdir}"/0013-MSYS2-Prepend-getMSYS2Root-to-file-checks.patch
  git am --committer-date-is-author-date "${srcdir}"/0014-MSYS2-Support-discoverBinaryPath.patch
}

build() {
  local _config
  cd ${srcdir}
  if check_option "debug" "n"; then
    _config="release"
  else
    _config="debug"
  fi

  rm -rf build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}

  msg "Building Octopi..."
  ${_qmake} ../${_gitname}/${_gitname}.pro CONFIG+="c++11 ${_config}"
  make ${_config}

  mkdir -p notifier/pacmanhelper
  pushd notifier/pacmanhelper
  msg "Building pacmanhelper..."
  ${_qmake} ../../../${_gitname}/notifier/pacmanhelper/pacmanhelper.pro CONFIG+="${_config}"
  make ${_config}
  popd

  mkdir -p notifier/${_gitname}-notifier
  pushd notifier/${_gitname}-notifier
  msg "Building ${_gitname}-notifier..."
  ${_qmake} ../../../${_gitname}/notifier/${_gitname}-notifier/${_gitname}-notifier.pro CONFIG+="release"
  make ${_config}
  popd
}

package() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}

  cd "${srcdir}/build-${MINGW_CHOST}"

  install -D -m755 bin/${_gitname}                                            ${pkgdir}${MINGW_PREFIX}/bin/${_gitname}
  install -D -m755 notifier/bin/${_gitname}-notifier                          ${pkgdir}${MINGW_PREFIX}/bin/${_gitname}-notifier
  install -D -m644 ${srcdir}/${_gitname}/resources/images/${_gitname}_red.png ${pkgdir}${MINGW_PREFIX}/share/icons/$}_gitname}.png

  #services
  install -D -m755 notifier/bin/pacmanhelper                                  ${pkgdir}${MINGW_PREFIX}/lib/octopi/pacmanhelper

  install -D -m644 ${srcdir}/${_gitname}/notifier/pacmanhelper/polkit/org.octopi.pacman.policy        ${pkgdir}${MINGW_PREFIX}/share/polkit-1/actions/org.octopi.pacman.policy
  install -D -m644 ${srcdir}/${_gitname}/notifier/pacmanhelper/polkit/org.octopi.pacmanhelper.conf    ${pkgdir}${MINGW_PREFIX}/etc/dbus-1/system.d/org.octopi.pacmanhelper.conf
  install -D -m644 ${srcdir}/${_gitname}/notifier/pacmanhelper/polkit/org.octopi.pacmanhelper.xml     ${pkgdir}${MINGW_PREFIX}/share/dbus-1/interfaces/org.octopi.pacmanhelper.xml
  install -D -m644 ${srcdir}/${_gitname}/notifier/pacmanhelper/polkit/org.octopi.pacmanhelper.service ${pkgdir}${MINGW_PREFIX}/share/dbus-1/system-services/org.octopi.pacmanhelper.service
}
